//
//  SFExpandableTableViewHeaderView.m
//  TableViewFramework
//
//  Created by 支晓辉 on 16/3/24.
//  Copyright © 2016年 wishselfly. All rights reserved.
//

#import "SFExpandableTableViewHeaderView.h"

@interface SFExpandableTableViewHeaderView ()
/** 标题文本视图 */
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

@implementation SFExpandableTableViewHeaderView

- (instancetype)init {
    if (self = [super init]) {
        [self initHeaderView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initHeaderView];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self initHeaderView];
    }
    return self;
}

#pragma mark - Public methods

- (void)willChangeExpandableImageView {
    self.expandableImageView.layer.transform = _expanded ?
    CATransform3DRotate(CATransform3DIdentity, M_PI_2, 0.f, 0.f, -1.f) : CATransform3DIdentity;
}

#pragma mark - Private methods

- (void)initHeaderView {
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealWithHeaderViewTap)];
    [self addGestureRecognizer:gesture];
}

#pragma mark Gesture action

- (void)dealWithHeaderViewTap {
    if (self.tapHeaderViewActionBlock) {
        self.tapHeaderViewActionBlock();
    }
}

#pragma mark - Getters

- (NSString *)title {
    return self.titleLabel.text;
}

- (BOOL)showExpandIcon {
    return !self.expandableImageView.hidden;
}

#pragma mark - Setters

- (void)setExpanded:(BOOL)expanded animated:(BOOL)animated {
    _expanded = expanded;
    [UIView animateWithDuration:animated ? 0.2f:0.f animations:^{
        [self willChangeExpandableImageView];
    }];
}

- (void)setExpanded:(BOOL)expanded {
    [self setExpanded:expanded animated:YES];
}

- (void)setShowExpandIcon:(BOOL)showExpandIcon {
    self.expandableImageView.hidden = !showExpandIcon;
}

- (void)setTitle:(NSString *)title {
    self.titleLabel.text = title;
}

@end
